package inet.app.vod.activity.account;

import inet.app.vod.activity.R;
import inet.app.vod.activity.common.BaseActivity;
import inet.app.vod.net.http.VolleyClient;
import inet.app.vod.net.http.VolleyListener;
import inet.app.vod.object.ResultJsonModel;
import inet.app.vod.object.User;
import inet.app.vod.parser.Parse;
import inet.app.vod.util.MyPreferenceManager;
import inet.app.vod.util.MyUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.android.volley.VolleyError;
import com.inet.app.framework.util.hardware.KeyboardUtil;
import com.inet.app.framework.util.text.ValidateUtil;
import com.inet.app.framework.widget.MyToast;

public class LoginActivity extends BaseActivity implements OnClickListener {

	private EditText mUsernameEdt;
	private EditText mPasswordEdt;
	private View mErrorView;

	private String mUsername;
	private String mPassword;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_login);
		initUIBase();
		initViews();
	}

	private void initViews() {
		mUsernameEdt = (EditText) findViewById(R.id.edt_username);
		mPasswordEdt = (EditText) findViewById(R.id.edt_password);
		findViewById(R.id.bt_login).setOnClickListener(this);
		setTitle(getResources().getString(R.string.dang_nhap));
		showRightButton(false);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.bt_login:

			boolean cancel = validateField();
			if (cancel) {
				mErrorView.requestFocus();
				return;
			} else {
				KeyboardUtil.hideSoftKeyboard(this);
			}
			implementLogin();
			break;
		default:
			break;
		}

	}

	public void implementLogin() {
		VolleyClient.login(mUsername, mPassword, new VolleyListener(this) {

			@Override
			public void onSuccess(String response) {
				ResultJsonModel jResult = Parse.parseAccount(response);
				if (jResult == null) {
					// error parser
					MyUtil.showDialog(getString(R.string.loi_dang_nhap),
							LoginActivity.this);
				} else if (jResult.hasError()) {
					// error register
					String error = jResult.getError();
					MyUtil.showDialog(error, LoginActivity.this);
				} else {
					// register success
					MyToast.show(LoginActivity.this,
							getString(R.string.dang_nhap_thanh_cong));
					// Save account to preference
					User user = (User) jResult.getResult();
					user.setPassword(mPassword);
					MyPreferenceManager.getInstance(
							LoginActivity.this.getApplicationContext())
							.setAccount(user);
					finish();
				}
			}

			@Override
			public void onStart() {
				showLoading(true);
			}

			@Override
			public void onFinish() {
				showLoading(false);
			}

			@Override
			public void onError(VolleyError error) {
				MyUtil.showDialog(getString(R.string.loi_dang_nhap) + " ("
						+ error.getMessage() + ")", LoginActivity.this);
			}
		});

	}

	/**
	 * Check valid username and password
	 * 
	 * @return
	 */
	private boolean validateField() {
		// Reset errors
		mUsernameEdt.setError(null);
		mPasswordEdt.setError(null);

		mUsername = mUsernameEdt.getText().toString();
		mPassword = mPasswordEdt.getText().toString();

		boolean cancel = false;

		// Check valid username
		if (TextUtils.isEmpty(mUsername)) {
			mUsernameEdt
					.setError(getString(R.string.ten_dang_nhap_khong_duoc_de_trong));
			mErrorView = mUsernameEdt;
			cancel = true;
			return cancel;
		} else if (!ValidateUtil.validateUserName(mUsername)) {
			mUsernameEdt
					.setError(getString(R.string.ten_dang_nhap_khong_hop_le));
			mErrorView = mUsernameEdt;
			cancel = true;
			return cancel;
		}

		// Check valid password
		if (TextUtils.isEmpty(mPassword) || mPassword.length() < 6) {
			mPasswordEdt.setError(getString(R.string.mat_khau_it_nhat_6_ki_tu));
			mErrorView = mPasswordEdt;
			cancel = true;
			return cancel;
		}
		return cancel;
	}

	@Override
	public void setTagMenu() {
		mTagMenu = -1;
	}

}
